package com.example.demo;

import cn.hutool.core.util.ObjectUtil;
import com.example.demo.fun.TwoRunnable;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class FunUtil {

    public static TwoRunnable isTureOrFalse(boolean b){
        return (r1, r2) -> {
            if (b){
                r1.run();
            } else {
                r2.run();
            }
        };
    }

    public static <T>TwoRunnable isNull(T t){

        return (r1, r2) -> {
            if (ObjectUtil.isEmpty(t)){
                r1.run();
            } else {
                r2.run();
            }
        };
    }

    public static <T, X extends Throwable> void orNullThrow(T value, Consumer<T> consumer, Supplier<? extends X> exceptionSupplier) throws X {
        if (value != null) {
            consumer.accept(value);
        } else {
            throw exceptionSupplier.get();
        }
    }

}
